package com.qs.eggs.wxapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import com.google.gson.Gson;
import com.qs.eggs.R;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.HashMap;

import okhttp3.Call;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {

	}

	@Override
	public void onResp(BaseResp resp) {
	     //	Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

		//errCode为：0表示成功 -1表示错误 -2表示用户取消
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_tip);
//			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//			builder.show();
			if(resp.errCode==0){
				Toast.makeText(this,"支付成功",Toast.LENGTH_SHORT).show();
				afterPay();
				finish();
				WXPayActivity.instance.finish();

			}else if(resp.errCode==-1){
				finish();
				WXPayActivity.instance.finish();
				Toast.makeText(this,"支付失败",Toast.LENGTH_SHORT).show();
			}else if(resp.errCode==-2){
				finish();
				WXPayActivity.instance.finish();
				Toast.makeText(this,"支付取消",Toast.LENGTH_SHORT).show();
			}
		}
	}

	/**
	 * 确认支付之后的状态
	 */
	private void afterPay(){
		SharedPreferences preferences=getSharedPreferences("orderinfo", Context.MODE_PRIVATE);
		final String orderNo=preferences.getString("orderNo","");
		String orderMoney=preferences.getString("orderMoney","9999999");
		String custid=preferences.getString("custid","");
		HashMap<String,String> map=new HashMap<>();
		map.put("orderno",orderNo);
		map.put("payno","");
		map.put("money",orderMoney);
		map.put("custid",custid);
		map.put("paymethod","0");//支付方式
		Gson gson=new Gson();
		String parmas=gson.toJson(map);
//		OkHttpUtils
//				.post()
//				.url(Config.url2+"order/payOrder.do")
//				.addParams("data",parmas)
//				.addParams("mobile","true")
//				.build()
//				.execute(new StringCallback() {
//					@Override
//					public void onError(Call call, Exception e, int id) {
//						//	Toast.makeText(WXPayActivity.this,"链接服务器失败",Toast.LENGTH_SHORT).show();
//					}
//
//					@Override
//					public void onResponse(String response, int id) {
//						if(response.equals("true")){
//							Intent intent=new Intent(WXPayEntryActivity.this,ShouhuoActivity.class);
//							intent.putExtra("orderno",orderNo);
//							intent.putExtra("isgolds","0");
//							intent.putExtra("status","6");
//							startActivity(intent);
//							if(PayMActivity.instance!=null){
//								PayMActivity.instance.finish();
//							}
//							if(InitOrder.instance!=null){
//								InitOrder.instance.finish();
//							}
//							//	Toast.makeText(WXPayActivity.this,"确认付款成功",Toast.LENGTH_SHORT).show();
//						}else {
//							//	Toast.makeText(WXPayActivity.this,"确认付款失败",Toast.LENGTH_SHORT).show();
//						}
//
//					}
//				});
	}

}